<?php

set_include_path(
    implode(PATH_SEPARATOR,
        array(
            get_include_path()
    )));

require_once 'components/grid_view.php';

require_once 'components/vertical_grid_processor.php';
require_once 'components/input_values.php';

require_once 'database_engine/common_engine.php';
require_once 'database_engine/datareader_stub.php';

$pageInput = new PageInput();

if (false)
{
    $data = new EditableDataReaderStub(
        array(
            array("id" => 1, "value" => "string value 1"),
            array("id" => 2, "value" => "string value 2")
        ),
        array("id")
    );

    $data->SaveToFile('demo_data');
}

$data = EditableDataReaderStub::CreateFromFile('demo_data');

$renderer = new Renderer("components/templates");
$grid = new GridView('main_grid', $renderer, $data);

$column0 = $grid->AddColumn(new CustomRendererColumn());

$column1 = $grid->AddFieldColumn("id");
$columnViewProperties1 = new PlainTextViewProperties();
$column1->SetViewProperties($columnViewProperties1);

$column2 = $grid->AddFieldColumn("value");
$columnViewProperties2 = new PlainTextViewProperties();
$column2->SetViewProperties($columnViewProperties2);

class MyVerticalGridProcessor extends VerticalGridProcessor
{
    protected function DoCreateComponent()
    {
        include_once 'components/vertical_grid.php';

        $editGrid = new VerticalGrid($this->GetDataset());

        $row1 = $editGrid->AddFieldRow('Id', 'id');
        $rowProperties1 = new TextEditProperties('id_edit');
        $row1->SetEditProperties($rowProperties1);

        $row2 = $editGrid->AddFieldRow('Value', 'value');
        $rowProperties2 = new TextEditProperties('value_edit');
        $row2->SetEditProperties($rowProperties2);

        return $editGrid;
    }

    protected function DoCreateDataset()
    {
        global $data;
        return $data;
    }
}
$verticalGridProcessor = new MyVerticalGridProcessor('vg', $renderer);

if (isset($_GET['c']))
{
    $verticalGridProcessor->AddOperation(new UpdateGridOperation('u01', array('id')));
    $verticalGridProcessor->AddOperation(new CommitInsertOperation());
    $verticalGridProcessor->AddOperation(new CommitUpdateOperation());

    if ($_GET['c'] == 'main_grid')
    {
        $grid->ProcessRequest();
        exit;
    }
    else if ($_GET['c'] == 'vertical_grid')
    {
        //header('Content-Type: text/xml');
        $response = $verticalGridProcessor->ProcessInput($pageInput);
        $response->Send();
        $data->SaveToFile('demo_data');
        exit;
    }
}

date_default_timezone_set('Asia/Yekaterinburg');

?>
<!DOCTYPE HTML>
<html>
    <head>
        <link href="components/css/pgui.grid_view.css" rel="stylesheet" />

        <script src="libs/jquery/jquery-1.5.2.min.js" type="text/javascript"></script>
        <script src="libs/jquery/jquery.form.js" type="text/javascript"></script>
        
        <script src="libs/moo4q/mootools-1.2.4-base.js" type="text/javascript"></script>
        <script src="libs/moo4q/Class.Mutators.jQuery.js" type="text/javascript"></script>

        <script src="components/js/jquery.grid_view.js" type="text/javascript"></script>
        <script src="components/js/jquery.grid_view.unobtrusive.js" type="text/javascript"></script>
        <script src="components/js/jquery.vertical_grid.js" type="text/javascript"></script>
        <script src="components/js/jquery.vertical_grid.unobtrusive.js" type="text/javascript"></script>

        <script src="components/js/jquery.unobtrusive_pgui.js" type="text/javascript"></script>

        <script type="text/javascript">
            $(function()
            {
                $('table#main_grid').SetDataFilter([
                    {
                        fieldName: "id",
                        value: "1",
                        operator: "="
                    }
                ]);
                $('table#main_grid').RefreshData();

                $('table#main_grid').OnSelectRow(function(event, row)
                {
                    var primaryKeys = row.GetPrimaryKeys();
                    $('.pgui-vertical-grid').vertical_grid('UpdateGrid', 'u01', [primaryKeys[0]]);
                });
            });

            $(function(){

                $('#toggle_selectable').click(function(event)
                {
                    event.preventDefault();
                    if ($(this).attr('selectable-value') == 'true')
                    {
                        $('table#main_grid').SetSelectableRows(false);
                        $(this).attr('selectable-value', 'false');
                    }
                    else
                    {
                        $('table#main_grid').SetSelectableRows(true);
                        $(this).attr('selectable-value', 'true');
                    }
                });

                $('#refresh_grid_data').click(function(event)
                {
                    event.preventDefault();
                    $('table#main_grid').RefreshData();
                });

                $('#update_grid').click(function(event){
                    event.preventDefault();
                    
                    $('.pgui-vertical-grid').vertical_grid('UpdateGrid', 'u01', [1]);
                })
            });
        </script>
    </head>
    <body>

    <header>
        Demo page
    </header>

    <?php echo $grid->RenderGridContainer(); ?>
    
    <a href="#" id="refresh_grid_data">Refresh</a><br>
    <a href="#" id="toggle_selectable">Toggle selectable</a>

    <?php
        $verticalGridProcessor->DisplayGrid();
    ?>

    <a href="#" id="update_grid">Update Grid</a>

    </body>
</html>